#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
调试路由注册问题
"""

import sys
import os

# 添加项目路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from app import app

def debug_routes():
    """调试路由注册"""
    print("🔍 检查Flask应用路由:")
    print("=" * 60)
    
    # 获取所有路由
    routes = []
    for rule in app.url_map.iter_rules():
        methods = ','.join(sorted(rule.methods - {'HEAD', 'OPTIONS'}))
        routes.append((rule.rule, methods, rule.endpoint))
    
    # 按路径排序
    routes.sort(key=lambda x: x[0])
    
    # 查找爱国情感分析相关路由
    patriotic_routes = []
    other_routes = []
    
    for rule, methods, endpoint in routes:
        if 'patriotic' in rule.lower() or 'patriotic' in endpoint.lower():
            patriotic_routes.append((rule, methods, endpoint))
        else:
            other_routes.append((rule, methods, endpoint))
    
    print(f"📊 爱国情感分析相关路由 ({len(patriotic_routes)} 个):")
    for rule, methods, endpoint in patriotic_routes:
        print(f"  {rule:<40} {methods:<10} -> {endpoint}")
    
    print(f"\n📋 其他路由 ({len(other_routes)} 个):")
    for rule, methods, endpoint in other_routes[:10]:  # 只显示前10个
        print(f"  {rule:<40} {methods:<10} -> {endpoint}")
    
    if len(other_routes) > 10:
        print(f"  ... 还有 {len(other_routes) - 10} 个其他路由")
    
    # 检查蓝图注册
    print(f"\n🔧 已注册的蓝图:")
    for blueprint_name, blueprint in app.blueprints.items():
        print(f"  {blueprint_name}: {blueprint.url_prefix}")
    
    # 测试特定路由
    print(f"\n🧪 测试特定路由:")
    test_routes = [
        '/patriotic_prompt/',
        '/patriotic_prompt/batch_analyze',
        '/patriotic_prompt/api/analyze_single'
    ]
    
    with app.test_client() as client:
        for route in test_routes:
            try:
                response = client.get(route)
                print(f"  {route:<40} -> 状态码: {response.status_code}")
                if response.status_code == 200:
                    content_preview = response.get_data(as_text=True)[:100].replace('\n', ' ')
                    print(f"    内容预览: {content_preview}...")
            except Exception as e:
                print(f"  {route:<40} -> 错误: {str(e)}")

if __name__ == "__main__":
    debug_routes()


























